package com.budwk.app.sqfw.services.communityservice.impl;

import com.budwk.app.sqfw.component.CommunityStreetComponent;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.models.SqfwNotice;
import com.budwk.app.sqfw.services.common.SqfwCustomForAdminCommonService;
import com.budwk.app.sqfw.services.communityservice.SqfwNoticeForAdminService;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfw.vo.GenerateWordAndPdfUrlVO;
import com.budwk.app.sqfw.vo.SqfwNoticeForAdminVO;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.adminUtils.CommonCheckUtils;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import java.util.List;

/**
 * @program: dfxy-community
 * @description: 社区公告服务实现（后台管理系统）
 * @author: LJT
 * @create: 2024-01-26 17:32
 **/
@IocBean(args = {"refer:dao"})
@Slf4j
public class SqfwNoticeForAdminServiceImpl extends BaseServiceImpl<SqfwNotice> implements SqfwNoticeForAdminService {
    public SqfwNoticeForAdminServiceImpl(Dao dao) {
        super(dao);
    }

    private static final String ZHISHU = "直属";// 直属

    @Inject
    SysUserService sysUserService;

    @Inject
    private CommunityStreetComponent communityStreetComponent;

    @Inject
    SqfwCustomForAdminCommonService sqfwCustomForAdminCommonService;

    @Override
    public Pagination list(SqfwNoticeForAdminVO sqfwNoticeForAdminVO, int pageNo, int pageSize, String pageOrderName, String pageOrderBy) {
        // 构造查询条件
        Criteria criteria = buildQueryCond(sqfwNoticeForAdminVO);
        return listPage(pageNo, pageSize, criteria);
    }

    @Override
    @Aop(TransAop.READ_COMMITTED)
    public int delete(String id) {
        int num = 0;
        Cnd cnd = Cnd.NEW();
        cnd.and(SqfwNotice::getNoticeId, "=", id);
        SqfwNotice sqfwNotice = fetch(cnd);
        if (sqfwNotice == null) {
            return num;
        }
        log.info("SqfwNoticeForAdminServiceImpl-delete-删除记录:{},{}", SecurityUtil.getUserId(), id);
        num = clear(cnd);
        return num;
    }

    @Override
    public SqfwNotice detail(String id) {
        Cnd cnd = Cnd.NEW();
        cnd.and(SqfwNotice::getNoticeId, "=", id);
        SqfwNotice sqfwNotice = fetch(cnd);
        if (sqfwNotice == null) {
            return new SqfwNotice();
        }
        return sqfwNotice;
    }

    @Override
    @Aop(TransAop.READ_COMMITTED)
    public int update(SqfwNoticeForAdminVO SqfwNoticeForAdminVO) {
        return 0;
    }

    @Override
    public List<GenerateExcelUrlVO> downloadExcel(List<ExcelCreateConfig> excelConfigList, SqfwNoticeForAdminVO sqfwNoticeForAdminVO) {
        return null;
    }

    @Override
    public GenerateWordAndPdfUrlVO downloadPdf(List<ExcelCreateConfig> excelConfigList, SqfwNoticeForAdminVO SqfwNoticeForAdminVO) {
        return null;
    }

    @Override
    public Criteria buildQueryCond(SqfwNoticeForAdminVO bean) {
        Criteria cri = Cnd.cri();
        SqlExpressionGroup criSql = cri.where();
        // 组装查询条件
        if (StringUtils.isNotBlank(bean.getPageOrderName()) && Strings.isNotBlank(bean.getPageOrderBy())) {
            if (bean.getPageOrderBy().equals(CommonConstants.ASC)) {
                cri.getOrderBy().asc(bean.getPageOrderName());
            }
            if (bean.getPageOrderBy().equals(CommonConstants.DESC)) {
                cri.getOrderBy().desc(bean.getPageOrderName());
            }
        } else {
            cri.getOrderBy().desc(SqfwNotice::getCreateTime);
        }
        // 公告标题
        if (StringUtils.isNotBlank(bean.getNoticeTitle())) {
            criSql.andLike(SqfwNotice::getNoticeTitle, bean.getNoticeTitle());
        }
        // 时间
        criSql.andBetween(SqfwNotice::getCreateTime,
                communityStreetComponent.handleStartQueryTime(bean.getQueryStartTime()),
                communityStreetComponent.handleEndQueryTime(bean.getQueryEndTime()));

        // 区域5级检查
        CommonCheckUtils.checkAreaRange(criSql);
        // 区(前端条件)
        if (StringUtils.isNotBlank(bean.getDistrict()) && !ZHISHU.equals(bean.getDistrict())) {
            criSql.andLike(SqfwNotice::getDistrict, bean.getDistrict());
        }
        // 街道(前端条件)
        if (StringUtils.isNotBlank(bean.getCounty()) && !ZHISHU.equals(bean.getCounty())) {
            criSql.andLike(SqfwNotice::getCounty, bean.getCounty());
        }
        // 辖区(前端条件)
        if (StringUtils.isNotBlank(bean.getCommunity()) && !ZHISHU.equals(bean.getCounty())) {
            criSql.andLike(SqfwNotice::getCommunity, bean.getCommunity());
        }
        // 发布人
        if (StringUtils.isNotBlank(bean.getCreateName())) {
            criSql.andLike(SqfwNotice::getCreateName, bean.getCreateName());
        }
        // 全局搜索
        if (StringUtils.isNotEmpty(bean.getQueryAll())) {
            SqlExpressionGroup noticeTitle = Cnd.exps(SqfwNotice::getNoticeTitle, "like", "%" + bean.getQueryAll() + "%");
            SqlExpressionGroup createName = Cnd.exps(SqfwNotice::getCreateName, "like", "%" + bean.getQueryAll() + "%");
            SqlExpressionGroup county = Cnd.exps(SqfwNotice::getCounty, "like", "%" + bean.getQueryAll() + "%");
            SqlExpressionGroup district = Cnd.exps(SqfwNotice::getDistrict, "like", "%" + bean.getQueryAll() + "%");
            SqlExpressionGroup community = Cnd.exps(SqfwNotice::getCommunity, "like", "%" + bean.getQueryAll() + "%");
            criSql.and(noticeTitle.or(createName).or(county).or(district).or(community));
        }
        return cri;
    }
}
